public class MySingleLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//代表链表的头结点

    //创建链表
    public void creatList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);

        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }
    //打印链表
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //获取链表的长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int val) {
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int val) {
        ListNode node = new ListNode(val);
        ListNode cur = head;
        if (head == null) {
            head = node;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //在任意位置插入val
    public void addIndex(int index, int val) {
        //1. 判断index的合法性
        try {
            checkIndex(index);
        } catch (IndexNotLegalException e) {
            e.printStackTrace();
        }
        //2. index==0 || index ==size()
        if (index == 0) {
            addFirst(val);
            return;
        }
        if (index == size()) {
            addLast(val);
            return;
        }
        //3. 找到index的前一个位置
        ListNode cur = findIndexSubOne(val);
        //4. 进行连接
        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findIndexSubOne(int index) {
        int count = 0;
        ListNode cur = head;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    private void checkIndex(int index) throws IndexNotLegalException {
        if (index < 0 || index > size()) {
            throw new IndexNotLegalException("index不合法");
        }
    }

    //查找是否包含关键字val是否在单链表中
    public boolean contains(int val) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为val的节点
    public void remove(int val) {
        if (head == null) {
            return;
        }
        if (head.val == val) {
            head = head.next;
            return;
        }
        //1.找到当前需要删除值的节点的前一个
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == val) {
                ListNode del = cur.next;
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有关键字为val的节点
    public void removeAllKey(int val) {
        //1. 判断头结点为空
        if (head == null) {
            return;
        }
        //2. 定义prev和cur
        ListNode prev = head;
        ListNode cur = head.next;
        //3. 开始判断并且删除
        while (cur != null) {
            if (cur.val == val) {
                prev.next = cur.next;
                //cur=cur.next;
            } else {
                prev = cur;//prev = prev.next
                //cur=cur.next;
            }
            cur = cur.next;
        }
        //4. 处理头节点
        if (head.val == val) {
            head = head.next;
        }
    }

    //清空链表
    public void clear() {
        //head=null;
        ListNode cur = head;
        while (cur != null) {
            ListNode curN = cur.next;
            //cur.val =null;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }
}
